package com.baijiaoxi.utils.pinyin;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import org.apache.commons.codec.binary.Hex;

import java.io.*;

public class PinYinUtil {
    /**
     * 将汉字转换为全拼
     *
     * @param str
     * @return String
     */
    public static String getPinYin(String str) {
        HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
        // 默认小写
        outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        // 不显示拼音的声调
        outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        // outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

        StringBuilder sb = new StringBuilder();
        try {
            for (char c : str.toCharArray()) {
                // 如果包含有中文标点除号，需要使用正则表达式
                if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
                    // if (c > 128) {
                    sb.append(PinyinHelper.toHanyuPinyinStringArray(c,
                            outputFormat)[0]);
                } else {
                    sb.append(Character.toString(c));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 提取每个汉字的首字母
     *
     * @param str
     * @return String
     */
    public static String getPinYinFirstChar(String str) {
        StringBuilder sb = new StringBuilder();
        for (char c : str.toCharArray()) {
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
            if (pinyinArray != null) {
                sb.append(pinyinArray[0].charAt(0));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 提取第一个汉字的首字母
     *
     * @param str
     * @return String
     */
    public static String getFirstPinYinFirstChar(String str) {
        return String.valueOf(PinyinHelper.toHanyuPinyinStringArray(str.toCharArray()[0])[0].charAt(0)).toUpperCase();
    }

    /**
     * 将汉字转 Unicode 码
     *
     * @param str
     * @return String
     */
    public static String toUnicode(String str) {
        StringBuilder sb = new StringBuilder();
        int len = str.length();
        for (int i = 0; i < len; i++) {
            // 将每个字符转换成ASCII码
            sb.append(Integer.toHexString(str.charAt(i) & 0xffff) + "\\u");
        }
        return sb.toString();
    }

    public static String decodeHexStr(int dataCoding, String hexStr) {
        String realStr = null;
        try {
            if (hexStr != null) {
                if (dataCoding == 15) {
                    realStr = new String(Hex.decodeHex(hexStr.toCharArray()), "GBK");
                } else if ((dataCoding & 0x0C) == 0x08) {
                    realStr = new String(Hex.decodeHex(hexStr.toCharArray()), "UnicodeBigUnmarked");
                } else {
                    realStr = new String(Hex.decodeHex(hexStr.toCharArray()), "ISO8859-1");
                }
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }

        return realStr;
    }

    public static void main(String[] args) throws Exception {
    /*    String str = "china.史";
        System.out.println(getPinYin(str));
        System.out.println(getPinYinFirstChar(str));
        System.out.println(toUnicode("中JAVA大"));*/
       /* String time = "2020-08-01";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println((new Date().getTime() - dateTime.getTime()) / (float) 86400000);
        System.out.println(Math.round((new Date().getTime() - dateTime.getTime()) / (float) 86400000));*/
        // sub("D:\\project\\rongge\\rongge.sql");
        System.out.println(String.format("-%04d", 1));
    }

    private static void sub(String filePath) {
        BufferedReader br = null;
        String line = null;
        StringBuffer buf = new StringBuffer();

        try {
            // 根据文件路径创建缓冲输入流
            br = new BufferedReader(new FileReader(filePath));
            int i = 0;
            // 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
            while ((line = br.readLine()) != null) {
                if (i >= 579250) {
                    buf.append(line);
                    buf.append(System.getProperty("line.separator"));
                }
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    br = null;
                }
            }
        }

        BufferedWriter bw = null;

        try {
            // 根据文件路径创建缓冲输出流
            bw = new BufferedWriter(new FileWriter(filePath + "1"));
            // 将内容写入文件中
            bw.write(buf.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    bw = null;
                }
            }
        }
    }
}